﻿using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormQueryProductType : FormTemplet
    {
        public FormQueryProductType()
        {
            this.InitializeComponent();
        }

        private void FormQueryCustomer_Load(object sender, EventArgs e)
        {
            this.cbCondition.SelectedIndex = 0;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string text = "SELECT PKID AS 序号,[Name] AS 类型名称,Thick AS 厚度规格,Unit AS 单位,DefaultPrice as 默认价格,LabelMultiple as 标签倍数,Remark AS 备注,LimitedQuantity as 流程卡数量上限,LimitedArea as 流程卡面积上限 FROM ProductType";
            if (this.cbCondition.SelectedIndex == 0)
            {
                if (this.rbChinese.Checked)
                {
                    text = text + " WHERE [Name] LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
                }
                else
                {
                    text = text + " WHERE dbo.GetPY([Name]) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
                }
            }
            if (this.cbCondition.SelectedIndex == 1)
            {
                if (this.rbChinese.Checked)
                {
                    text = text + " WHERE Thick LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
                }
                else
                {
                    text = text + " WHERE dbo.GetPY(Thick) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
                }
            }
            if (this.cbCondition.SelectedIndex == 2)
            {
                if (this.rbChinese.Checked)
                {
                    text = text + " WHERE Remark LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
                }
                else
                {
                    text = text + " WHERE dbo.GetPY(Remark) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
                }
            }
            text += " order by [Sequence]";
            ProductTypes productTypes = new ProductTypes();
            DataTable dataTable = null;
            try
            {
                dataTable = productTypes.GetProductTypes(text);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n出现未知错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            productTypes.Dispose();
            if (dataTable != null)
            {
                this.dgProductType.DataSource = null;
                this.dgProductType.DataSource = dataTable;
                this.dgProductType.AutoResizeColumns();
                dataTable.Dispose();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("更改类型后，之前使用此类型的数据将随之改变，请慎重操作！\r\n\r\n是否继续更改？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != DialogResult.No)
            {
                try
                {
                    string text = this.dgProductType.Rows[0].Cells[0].Value.ToString();
                }
                catch (Exception)
                {
                    MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                int rowIndex = this.dgProductType.CurrentCell.RowIndex;
                new FormUpdateProductType(Convert.ToInt32(this.dgProductType.Rows[rowIndex].Cells[0].Value))
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
